/**
 * 
 */
package com.dsteiner.xelavos.client.model;

import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;

import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.domain.BaseObject;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.xelavos.client.domain.Job;
import com.dsteiner.xelavos.client.domain.Leisure;
import com.dsteiner.xelavos.client.domain.Product;
import com.dsteiner.xelavos.client.domain.Service;
import com.dsteiner.xelavos.client.domain.User;

public class MainTabModel {
	
	public static class Tab extends BaseObject{
		private int id;
		private String name;
		private String className;
		public Tab() {
		}
		private Tab(int id, String name, String className) {
			this.id = id;
			this.name = name;
			this.className = className;
		}
		public String getName() {
			return name;
		}
		public String getClassName() {
			return className;
		}
		public int getId() {
			return id;
		}
		public boolean equals(Object obj) {
			if (obj == null)
				return false;
			if (!(obj instanceof Tab))
				return false;
			Tab other = (Tab) obj;
			return id == other.id;
		}
	}
	
	private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
			this);

	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.registerPropertyChangeListener(propertyName,
				listener);
	}
	public final static Tab TAB_ID_SHOPPING = new Tab(1, "compras", Product.class.getName());
	public final static Tab TAB_ID_JOB = new Tab(3, "trabajo", Job.class.getName());
	public final static Tab TAB_ID_LEISURE =  new Tab(4, "recreacion", Leisure.class.getName());
	public final static Tab TAB_ID_SERVICES =  new Tab(2, "servicios", Service.class.getName());
	public final static Tab TAB_ID_COMMUNITY =  new Tab(5, "comunidad", User.class.getName());
	public final static Tab[] tabs = new Tab[]{TAB_ID_SHOPPING, TAB_ID_SERVICES, TAB_ID_JOB, TAB_ID_LEISURE, TAB_ID_COMMUNITY};
	public final static Map<String, Tab> classNameTabLookup = new HashMap<String, Tab>();
	public final static Map<Integer, Tab> idTabLookup = new HashMap<Integer, Tab>();
	
	static{
		for (Tab tab : tabs) {
			classNameTabLookup.put(tab.getClassName(), tab);
			idTabLookup.put(tab.getId(), tab);
		}
	}
	
	private Tab selectedTab = TAB_ID_SHOPPING;

	public Tab getSelectedTab() {
		return selectedTab;
	}

	public void setSelectedTab(Tab selectedTab) {
		Tab oldSelectedTab = this.selectedTab;
		this.selectedTab = selectedTab;
		propertyChangeSupport.firePropertyChange("selectedTab", oldSelectedTab, selectedTab);
	}

	public boolean isShoppingTabSelected() {
		return selectedTab == TAB_ID_SHOPPING;
	}
	public boolean isJobsTabSelected() {
		return selectedTab == TAB_ID_JOB;
	}
	public boolean isLeisureTabSelected() {
		return selectedTab == TAB_ID_LEISURE;
	}
	public boolean isFriendsTabSelected() {
		return selectedTab == TAB_ID_COMMUNITY;
	}
	public boolean isServicesTabSelected() {
		return selectedTab == TAB_ID_SERVICES;
	}

	public static Tab getTab(String className) {
		return classNameTabLookup.get(className);
	}
	
	public static Tab getTab(ModelObject modelObject) {
		return getTab(modelObject.getClass().getName());
	}

	public static Tab getTab(int id) {
		return idTabLookup.get(id);
	}

}